#include "Texture.h"
#include <assert.h>
CTexture::CTexture():m_texture(0), m_spriteWidth(0), m_spriteHeight(0), m_nbOfSprite(0), m_nbOfSpritePerRow(0){}

CTexture::~CTexture()
{
	if(m_texture!=0)
		SDL_FreeSurface(m_texture);
}

bool CTexture::loadTexture(const string& filename, int spriteWidth, int spriteHeight, int nbOfSprite, int nbOfSpritePerRow)
{
	//load the bitmap
	SDL_Surface* m_temp = SDL_LoadBMP(filename.c_str());
	if(m_temp == 0)
		return false;

	//convert it into a format that can be displayed
	m_texture = SDL_DisplayFormat(m_temp);
	if(m_texture == 0)
		return false;

	//delete the temporary surface
	SDL_FreeSurface(m_temp);
	m_temp=0;

	//set the informations about the sprites
	m_spriteWidth=spriteWidth;
	m_spriteHeight=spriteHeight;
	m_nbOfSprite=nbOfSprite;
	m_nbOfSpritePerRow=nbOfSpritePerRow;

	//set the color keying
	SDL_SetColorKey(m_texture, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(m_texture->format, KEYCOLOR));
	return true;
}

bool CTexture::loadTextureFromMemory(char* buffer, int fileSize, int spriteWidth, int spriteHeight,	int nbOfSprite, 
		int nbOfSpritePerRow)
{
	//get data from file
	SDL_RWops* file;
	file = SDL_RWFromMem(buffer, fileSize);

	//load bmd from file
	SDL_Surface* temp;
	temp = SDL_LoadBMP_RW(file, 1);

	if(temp == 0)
		return false;

	//convert it into a format that can be displayed
	m_texture = SDL_DisplayFormat(temp);
	if(m_texture == 0)
		return false;

	//delete the temporary surface
	SDL_FreeSurface(temp);
	temp=0;

	//set the informations about the sprites
	m_spriteWidth=spriteWidth;
	m_spriteHeight=spriteHeight;
	m_nbOfSprite=nbOfSprite;
	m_nbOfSpritePerRow=nbOfSpritePerRow;

	//set the color keying
	SDL_SetColorKey(m_texture, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(m_texture->format, KEYCOLOR));
	return true;
}

void CTexture::displayTexture(const CVector2i& position, SDL_Surface* screen)const
{
	assert(screen != 0);

	//create the destination rectangle
	SDL_Rect destination;
	destination.x = (Sint16)position.getX();
	destination.y = (Sint16)position.getY();
	destination.h = (Sint16)m_texture->h;
	destination.w = (Sint16)m_texture->w;
	
	//draw the texture
	SDL_BlitSurface(m_texture, NULL, screen, &destination);
}

void CTexture::displaySprite(const CVector2i& position, int indexSprite, SDL_Surface* screen)const
{
	assert(screen != 0);
	assert(indexSprite>=0);

	//get the rectangle that defines the sprite
	SDL_Rect spriteRectangle;
	spriteRectangle.x = (Sint16)((indexSprite) % (m_nbOfSpritePerRow)*m_spriteWidth);
	spriteRectangle.w = (Sint16)m_spriteWidth;
	spriteRectangle.y = (Sint16)(m_spriteHeight * (indexSprite/m_nbOfSpritePerRow));
	spriteRectangle.h = (Sint16)m_spriteHeight;

	//get the destination rectangle
	SDL_Rect destination;
	destination.x = (Sint16)position.getX();
	destination.y = (Sint16)position.getY();
	destination.h = (Sint16)m_spriteWidth;
	destination.w = (Sint16)m_spriteHeight;

	//draw the sprite
	SDL_BlitSurface(m_texture, &spriteRectangle, screen, &destination);
}

int CTexture::getSpriteWidth(void)const
{
	return m_spriteWidth;
}

int CTexture::getSpriteHeight(void)const
{
	return m_spriteHeight;
}

int CTexture::getTextureWidth(void)const
{
	return m_texture->w;
}

int CTexture::getTextureHeight(void)const
{
	return m_texture->h;
}
